package com.accessibleworlds.report;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.accessibleworlds.R;
import com.accessibleworlds.maps.MapPinpoint;
import com.accessibleworlds.user.Options;
import com.accessibleworlds.user.UserPage;
import com.accessibleworlds.util.Connections;
import com.accessibleworlds.util.SharedVariables;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class CreateReport extends MapActivity{

	private MapView map;
	private MapController controller;
	private MyLocationOverlay compass;
	private List<Overlay> overlayList;

	private GeoPoint touchedPoint, p;

	private ArrayList<ReportInformation> allReports;

	private Drawable star,blackstar,friendly,hole,yellowhole,orangehole,redhole,nopedestrian,noWheelchair,yellowWheel,orangeWheel,redWheel,noelevator,yellowElevator,orangeElevator,redElevator,yellowNoAcross,orangeNoAcross,redNoAcross,reconstruction,yellowSign,orangeSign,redSign;


	private MapPinpoint pinpoint,location,friendlyReportPoints,holeReportPoints,yellowholeReportPoints,orangeholeReportPoints,redholeReportPoints,noPedestrianRoadReportPoints,noWheelchairRampReportPoints,yellowWheelReportPoints, orangeWheelReportPoints,redWheelReportPoints,noelevatorReportPoints,yellowElevatorReportPoints,orangeElevatorReportPoints,redElevatorReportPoints,yellowNoAcrossReportPoints,orangeNoAcrossReportPoints,redNoAcrossReportPoints,reconstructionReportPoints,yellowSignReportPoints,orangeSignReportPoints,redSignReportPoints;

	private boolean isVisible;

	private ProgressDialog progressDialog;

	private Dialog alertDialog;

	private int ID_MENU_Search = 5, ID_MENU_Current = 4;
	private Geocoder geoCoder;

	private double latitude= 0,longitude= 0;

	private LocationManager mlocManager;

	private LocationListener mlocListener;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.createreport);

		isVisible = true;

		initializeVariables();
		changeLanguage();

		new GetReportsTask().execute();

	}

	@Override
	protected void onResume() {
		super.onResume();
		compass.enableCompass();
		isVisible=true;
		changeLanguage();
	}

	@Override
	protected void onPause() {
		super.onPause();
		compass.disableCompass();
		isVisible=false;
	}

	@Override
	public void onBackPressed() {
		Intent intent = new Intent(this,UserPage.class);
		Bundle bundle = new Bundle();
		bundle.putString("previousID", SharedVariables.creatReportID);
		intent.putExtras(bundle);
		if(isVisible) {
			startActivity(intent);
			overridePendingTransition(R.anim.fade, R.anim.hold);
		}
		finish();
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	private void initializeVariables() {
		map = (MapView) findViewById(R.id.createReportMap);
		map.setBuiltInZoomControls(true);

		CreateReportOverlay createReportOverlay = new CreateReportOverlay();
		overlayList = map.getOverlays();
		overlayList.add(createReportOverlay);

		compass = new MyLocationOverlay(CreateReport.this, map);
		overlayList.add(compass);

		controller = map.getController();

		GeoPoint point = new GeoPoint(41086147, 29048055);
		controller.animateTo(point);
		controller.setZoom(16);

		//getting icons from R.java
		star = getResources().getDrawable(R.drawable.yellow_star);
		blackstar = getResources().getDrawable(R.drawable.black_star);
		friendly= getResources().getDrawable(R.drawable.friendly);
		hole = getResources().getDrawable(R.drawable.hole);
		yellowhole= getResources().getDrawable(R.drawable.yellowhole);
		orangehole= getResources().getDrawable(R.drawable.orangehole);
		redhole= getResources().getDrawable(R.drawable.redhole);
		nopedestrian = getResources().getDrawable(R.drawable.nopedestrian);
		noWheelchair = getResources().getDrawable(R.drawable.nowheelchair);
		yellowWheel= getResources().getDrawable(R.drawable.yellowwheel);
		orangeWheel = getResources().getDrawable(R.drawable.orangewheel);
		redWheel = getResources().getDrawable(R.drawable.redwheel);
		noelevator = getResources().getDrawable(R.drawable.noelevator);
		yellowElevator = getResources().getDrawable(R.drawable.noelevatoryellow);
		orangeElevator = getResources().getDrawable(R.drawable.noelevatororange);
		redElevator = getResources().getDrawable(R.drawable.noelevatorred);
		yellowNoAcross = getResources().getDrawable(R.drawable.noacrossyellow);
		orangeNoAcross = getResources().getDrawable(R.drawable.noacrossorange);
		redNoAcross = getResources().getDrawable(R.drawable.noacrossred);
		reconstruction = getResources().getDrawable(R.drawable.reconstruction);
		yellowSign = getResources().getDrawable(R.drawable.signyellow);
		orangeSign = getResources().getDrawable(R.drawable.orangesign);
		redSign = getResources().getDrawable(R.drawable.signred);

		//friendly location for disabled people
		friendlyReportPoints = new MapPinpoint(friendly);
		overlayList.add(friendlyReportPoints);
		
		//hole is a violation type that corresponds to holes in a street or road
		holeReportPoints = new MapPinpoint(hole);
		overlayList.add(holeReportPoints);

		//yellow hole is a violation type which is a hole with normal status 
		yellowholeReportPoints = new MapPinpoint(yellowhole);
		overlayList.add(yellowholeReportPoints);

		//orange hole is a violation type which is a hole with minor status 
		orangeholeReportPoints = new MapPinpoint(orangehole);
		overlayList.add(orangeholeReportPoints);

		//red hole is a violation type which is a hole with urgent status 
		redholeReportPoints = new MapPinpoint(redhole);
		overlayList.add(redholeReportPoints);

		//noPedestrianRoad is a violation type that is related to pedestrian road problems
		noPedestrianRoadReportPoints = new MapPinpoint(nopedestrian);
		overlayList.add(noPedestrianRoadReportPoints);

		//noWheelchairRamp is a violation type that corresponds to unsuitable places for people with Wheel-chair
		noWheelchairRampReportPoints = new MapPinpoint(noWheelchair);
		overlayList.add(noWheelchairRampReportPoints);

		//yellow Wheel is a violation type which is a noWheelchairRamp with normal status
		yellowWheelReportPoints = new MapPinpoint(yellowWheel);
		overlayList.add(yellowWheelReportPoints);

		//orange Wheel is a violation type which is a noWheelchairRamp with minor status
		orangeWheelReportPoints = new MapPinpoint(orangeWheel);
		overlayList.add(orangeWheelReportPoints);

		//red Wheel is a violation type which is a noWheelchairRamp with urgent status
		redWheelReportPoints = new MapPinpoint(redWheel);
		overlayList.add(redWheelReportPoints);

		//no elevator is a violation type which corresponds to a place that has not an elevator for people with wheel-chair
		noelevatorReportPoints = new MapPinpoint(noelevator);
		overlayList.add(noelevatorReportPoints);

		//yellow Elevator is a violation type which is a no elevator violation with normal status
		yellowElevatorReportPoints = new MapPinpoint(yellowElevator);
		overlayList.add(yellowElevatorReportPoints);

		//orange Elevator is a violation type which is a no elevator violation with minor status
		orangeElevatorReportPoints = new MapPinpoint(orangeElevator);
		overlayList.add(orangeElevatorReportPoints);

		//red Elevator is a violation type which is a no elevator violation with urgent status
		redElevatorReportPoints = new MapPinpoint(redElevator);
		overlayList.add(redElevatorReportPoints);
		
		//yellow NoAcross is a violation type which means there is no way to across the street or road with normal status
		yellowNoAcrossReportPoints = new MapPinpoint(yellowNoAcross);
		overlayList.add(yellowNoAcrossReportPoints);

		//orange NoAcross is a violation type which means there is no way to across the street or road with minor status
		orangeNoAcrossReportPoints = new MapPinpoint(orangeNoAcross);
		overlayList.add(orangeNoAcrossReportPoints);

		//red NoAcross is a violation type which means there is no way to across the street or road with urgent status
		redNoAcrossReportPoints = new MapPinpoint(redNoAcross);
		overlayList.add(redNoAcrossReportPoints);
		
		//reconstruction is a violation type which is a reconstruction of a road or street violation
		reconstructionReportPoints = new MapPinpoint(reconstruction);
		overlayList.add(reconstructionReportPoints);

		//yellow Sign is a violation type which is an unsafe sign board violation with normal status
		yellowSignReportPoints = new MapPinpoint(yellowSign);
		overlayList.add(yellowSignReportPoints);

		//orange Sign is a violation type which is an unsafe sign board violation with minor status
		orangeSignReportPoints = new MapPinpoint(orangeSign);
		overlayList.add(orangeSignReportPoints);

		//red Sign is a violation type which is an unsafe sign board violation with urgent status
		redSignReportPoints = new MapPinpoint(redSign);
		overlayList.add(redSignReportPoints);

		pinpoint = new MapPinpoint(star);
		overlayList.add(pinpoint);

		location = new MapPinpoint(blackstar);
		overlayList.add(location);

		/* Use the LocationManager class to obtain GPS locations */
		mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
		Criteria criteria = new Criteria();
		String bestProvider = mlocManager.getBestProvider(criteria, false);
		
		Location myLocation = mlocManager.getLastKnownLocation(bestProvider);
		if(location!=null) {
			latitude = myLocation.getLatitude();
			longitude = myLocation.getLongitude();
		}

		mlocListener = new MyLocationListener();
		mlocManager.requestLocationUpdates( bestProvider, 500, 1, mlocListener);

		progressDialog = new ProgressDialog(CreateReport.this);
		progressDialog.setIndeterminate(true);
		progressDialog.setCancelable(false);
	}

	private void changeLanguage() {
		progressDialog.setMessage(getResources().getStringArray(R.array.loadingMessage)[SharedVariables.currentLanguage]);
	}

	/* Class My Location Listener */
	public class MyLocationListener implements LocationListener
	{
		public void onLocationChanged(Location loc)
		{
			latitude = loc.getLatitude();
			longitude = loc.getLongitude();
		}
		public void onProviderDisabled(String provider)
		{
			Toast.makeText( getApplicationContext(), "Gps Disabled. Please turn it on to find your current location", Toast.LENGTH_SHORT ).show();
		}
		public void onProviderEnabled(String provider)
		{
			Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
			/* Use the LocationManager class to obtain GPS locations */
			mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

			mlocListener = new MyLocationListener();
			mlocManager.requestLocationUpdates( provider, 500, 1, mlocListener);

		}
		public void onStatusChanged(String provider, int status, Bundle extras)
		{

		}
	}

	public class GetReportsTask extends AsyncTask<Void, Integer, String>{

		private String[] takenData = null;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressDialog.show();
		}

		@Override
		protected String doInBackground(Void... params) {

			if(Connections.serviceStatus()) {

				try {
					String url = Connections.URI+Connections.search;
					String [] parameters = {"accessCode"};
					String [] paramaterValues = {SharedVariables.accessCode};

					takenData = Connections.connection(url,parameters,paramaterValues);

					if("OK".equalsIgnoreCase(takenData[0])) {
						allReports = new ArrayList<ReportInformation>();
						for(int i=1;i<takenData.length;i++){
							JSONArray token = new JSONArray(takenData[i]);
							allReports.add(new ReportInformation(Integer.parseInt(token.getString(0)),
									token.getString(1),Integer.parseInt(token.getString(2)),
									token.getString(3),token.getString(4),
									Integer.parseInt(token.getString(5)), Integer.parseInt(token.getString(6)), Integer.parseInt(token.getString(7))));
						}
						publishProgress(1);
						return "OK";
					}
					else {
						publishProgress(2);
					}
				}
				catch (Exception ex) {
					publishProgress(2);
				}
			}
			else {
				publishProgress(0);
			}
			return "Fail";
		}

		protected void onProgressUpdate(Integer... progress) {
			if(progress[0] == 0) {
				progressDialog.dismiss();
				AlertDialog alertDialog = new AlertDialog.Builder(CreateReport.this).create();
				alertDialog.setTitle(getResources().getStringArray(R.array.connectionProblem)[SharedVariables.currentLanguage]);
				alertDialog.setMessage(getResources().getStringArray(R.array.connectionProblemException)[SharedVariables.currentLanguage]);
				alertDialog.setButton(getResources().getStringArray(R.array.tryAgain)[SharedVariables.currentLanguage], new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						new GetReportsTask().execute();
					}
				});
				alertDialog.setButton2(getResources().getStringArray(R.array.exit)[SharedVariables.currentLanguage], new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
				});
				alertDialog.show();
			}
			else if(progress[0]==1) {
				progressDialog.dismiss();
				for(int i=0;i<allReports.size();i++){
					ReportInformation reportInformation = allReports.get(i);
					GeoPoint previousPoints = new GeoPoint(reportInformation.latitude, reportInformation.longitude);
					OverlayItem overlayItem = new OverlayItem(previousPoints, "CreateReport", "addedPoint");

					//according to the report types (which are types of violations) set the corresponding icons
					//report types are 1=hole, 2=noPedestrianroad, 3=noWheelChairRamp, 4=noElevator, 5=noWaytoAccrossTheStreet, 6=reconstructionOfRoadorStreet, 7=unsafeSignBoard, 8=brokenPedestrianRoad
					//according to the report status color of the icon changes as yellow, orange or red which corresponds to urgency level: normal, minor and urgent respectively.
					if(allReports.get(i).type == 0){
						friendlyReportPoints.insertOverlayItem(overlayItem);
					}
					else if(allReports.get(i).type == 1) {
						if(allReports.get(i).status.equals("normal"))
							yellowholeReportPoints.insertOverlayItem(overlayItem);
						else if(allReports.get(i).status.equals("minor"))
							orangeholeReportPoints.insertOverlayItem(overlayItem);
						else if(allReports.get(i).status.equals("urgent"))
							redholeReportPoints.insertOverlayItem(overlayItem);
						else
							holeReportPoints.insertOverlayItem(overlayItem);
					}
					else if(allReports.get(i).type == 2) {
						noPedestrianRoadReportPoints.insertOverlayItem(overlayItem);
					}
					else if(allReports.get(i).type == 3) {
						if(allReports.get(i).status.equals("normal"))
							yellowWheelReportPoints.insertOverlayItem(overlayItem);
						else if(allReports.get(i).status.equals("minor"))
							orangeWheelReportPoints.insertOverlayItem(overlayItem);
						else if(allReports.get(i).status.equals("urgent"))
							redWheelReportPoints.insertOverlayItem(overlayItem);
						else
							noWheelchairRampReportPoints.insertOverlayItem(overlayItem);
					}
					else if(allReports.get(i).type == 4) {
						if(allReports.get(i).status.equals("normal"))
							yellowElevatorReportPoints.insertOverlayItem(overlayItem);
						else if(allReports.get(i).status.equals("minor"))
							orangeElevatorReportPoints.insertOverlayItem(overlayItem);
						else if(allReports.get(i).status.equals("urgent"))
							redElevatorReportPoints.insertOverlayItem(overlayItem);
						else
							noelevatorReportPoints.insertOverlayItem(overlayItem);
					}
					else if(allReports.get(i).type == 5) {
						if(allReports.get(i).status.equals("normal"))
							yellowNoAcrossReportPoints.insertOverlayItem(overlayItem);
						else if(allReports.get(i).status.equals("minor"))
							orangeNoAcrossReportPoints.insertOverlayItem(overlayItem);
						else
							redNoAcrossReportPoints.insertOverlayItem(overlayItem);
					}
					else if(allReports.get(i).type == 6) {
						reconstructionReportPoints.insertOverlayItem(overlayItem);
					}
					else if(allReports.get(i).type == 7) {
						if(allReports.get(i).status.equals("normal"))
							yellowSignReportPoints.insertOverlayItem(overlayItem);
						else if(allReports.get(i).status.equals("minor"))
							orangeSignReportPoints.insertOverlayItem(overlayItem);
						else
							redSignReportPoints.insertOverlayItem(overlayItem);
					}
					else noPedestrianRoadReportPoints.insertOverlayItem(overlayItem);
				}
				map.postInvalidate();
			}
			else if(progress[0]==2) {
				progressDialog.dismiss();
				Toast.makeText(CreateReport.this, getResources().getStringArray(R.array.connectionProblem)[SharedVariables.currentLanguage], Toast.LENGTH_LONG).show();
			}
		}

		@Override
		protected void onPostExecute(String result) {}
	}

	private class CreateReportOverlay extends Overlay {

		private long startTime;
		private long finishTime;

		@Override
		public boolean onTouchEvent(MotionEvent e, MapView mapView) {
			if(e.getAction()==MotionEvent.ACTION_DOWN) {
				startTime = System.currentTimeMillis();
				touchedPoint = map.getProjection().fromPixels((int) e.getX(), (int) e.getY());
				if(pinpoint.size()>0) {
					pinpoint.removeOverlayItem(0);
				}
				OverlayItem overlayItem = new OverlayItem(touchedPoint, "CreateReport", "touchedPoint");
				pinpoint.insertOverlayItem(overlayItem);
			}
			if(e.getAction()==MotionEvent.ACTION_UP) {
				finishTime = System.currentTimeMillis();
				if(finishTime - startTime >= 1000) {
					alertDialog = new Dialog(CreateReport.this);
					alertDialog.setContentView(R.layout.pop_up_with_two_button);
					alertDialog.setTitle("Create a Report");
					Button createReport = (Button) alertDialog.findViewById(R.id.twobuttons_choice1);
					createReport.setText("Create Violation Report");
					Button createFriendly = (Button) alertDialog.findViewById(R.id.twobuttons_choice2);
					createFriendly.setText("Create Disability Friendly Location Report");
					alertDialog.show();

					createReport.setOnClickListener(new OnClickListener() {
						public void onClick(View v) {
							Intent intent = new Intent(CreateReport.this,CreateViolationReport.class);
							Bundle bundle = new Bundle();
							bundle.putString("previousID", SharedVariables.creatReportID);
							bundle.putString("latitude", ""+touchedPoint.getLatitudeE6());
							bundle.putString("longitude", ""+touchedPoint.getLongitudeE6());
							intent.putExtras(bundle);
							if(isVisible) {
								startActivity(intent);
								overridePendingTransition(R.anim.fade, R.anim.hold);
							}
							alertDialog.dismiss();
							finish();
						}
					});
					createFriendly.setOnClickListener(new OnClickListener() {
						public void onClick(View v) {
							Intent intent = new Intent(CreateReport.this,CreateFriendlyLocationReport.class);
							Bundle bundle = new Bundle();
							bundle.putString("previousID", SharedVariables.creatReportID);
							bundle.putString("latitude", ""+touchedPoint.getLatitudeE6());
							bundle.putString("longitude", ""+touchedPoint.getLongitudeE6());
							intent.putExtras(bundle);
							if(isVisible) {
								startActivity(intent);
								overridePendingTransition(R.anim.fade, R.anim.hold);
							}
							alertDialog.dismiss();
							finish();
						}
					});
				}
			}
			return super.onTouchEvent(e, mapView);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater mainMenu = getMenuInflater();
		mainMenu.inflate(R.menu.mainmenu, menu);
		MenuItem item = menu.add(Menu.NONE, ID_MENU_Search, Menu.NONE, R.string.search);
		item.setShortcut('5', 'x');
		MenuItem item2 = menu.add(Menu.NONE, ID_MENU_Current, Menu.NONE, R.string.current);
		item2.setShortcut('4', 'c');
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		menu.findItem((R.id.menuOptions)).setTitle(getResources().getStringArray(R.array.options)[SharedVariables.currentLanguage]);
		menu.findItem(R.id.menuOptions).setIcon(R.drawable.optionsicon);
		menu.findItem((R.id.menuExit)).setTitle(getResources().getStringArray(R.array.exit)[SharedVariables.currentLanguage]);
		menu.findItem((R.id.menuExit)).setIcon(R.drawable.exiticon);
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId()==R.id.menuOptions) {
			Intent intent = new Intent(this,Options.class);
			Bundle bundle = new Bundle();
			bundle.putString("previousID", SharedVariables.creatReportID);
			intent.putExtras(bundle);
			startActivity(intent);
			overridePendingTransition(R.anim.fade, R.anim.hold);
		}
		else if (item.getItemId()==R.id.menuExit) {
			finish();
		}
		else if (item.getItemId()== ID_MENU_Search){
			alertDialog = new Dialog(this);
			alertDialog.setContentView(R.layout.message_to_admin);
			alertDialog.setTitle("Search Location");
			final EditText Topic = (EditText) alertDialog.findViewById(R.id.mesToAdminTopic);
			Topic.setHint("Enter your city");
			final EditText Message = (EditText) alertDialog.findViewById(R.id.mesToAdminMessage);
			Message.setHint("Enter your location");
			Button loc = (Button) alertDialog.findViewById(R.id.mesToAdminButton);
			loc.setText("Find location");
			alertDialog.show();
			loc.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					alertDialog.dismiss();
					List<Address> addresses=null;
					geoCoder = new Geocoder(getApplicationContext());
					try {
						addresses = geoCoder.getFromLocationName(Message.getText().toString()+" "+Topic.getText().toString(),5);
					} catch (IOException e) {
						e.printStackTrace();
					}
					System.out.println(addresses.size());
					for(int i=0;i<addresses.size();i++)
						System.out.println(addresses.get(i));
					if(addresses.size() > 0)
					{
						p = new GeoPoint( (int) (addresses.get(0).getLatitude() * 1E6), 
								(int) (addresses.get(0).getLongitude() * 1E6));

						controller.animateTo(p);
						controller.setZoom(20);
						if(location.size()>0) {
							location.removeOverlayItem(0);
						}
						OverlayItem currentLocation = new OverlayItem(p, "Current Location", "snip");
						location.insertOverlayItem(currentLocation);
					}
					else
					{
						AlertDialog.Builder adb = new AlertDialog.Builder(CreateReport.this);
						adb.setTitle("Google Map");
						adb.setMessage("Please Provide the Proper Place");
						adb.setPositiveButton("Close",null);
						adb.show();
					}
				}
			});
		}
		else if (item.getItemId()== ID_MENU_Current){
			if(latitude != 0 && longitude != 0){
				p = new GeoPoint( (int) (latitude * 1E6), 
						(int) (longitude * 1E6));

				controller.animateTo(p);
				controller.setZoom(20);
				if(location.size()>0) {
					location.removeOverlayItem(0);
				}
				OverlayItem currentLocation = new OverlayItem(p, "Current Location", "snip");
				location.insertOverlayItem(currentLocation);
			}
			else{
				AlertDialog.Builder adb = new AlertDialog.Builder(CreateReport.this);
				adb.setTitle("Google Map");
				adb.setMessage("If your GPS is closed please turn on or please try again later");
				adb.setPositiveButton("Close",null);
				adb.show();
			}
		}
		return super.onOptionsItemSelected(item);

	}
}
